package com.fansm.config;

import cn.hutool.extra.spring.SpringUtil;
import com.fansm.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

    @Autowired
    private DataSource dataSource;

    @Bean
    public UserDetailsService userDetailsService() {
        return new UserServiceImpl();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConditionalOnBean(DataSource.class)
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        return tokenRepository;
    }

    @Bean
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        UserDetailsService userDetailsService = SpringUtil.getBean("userDetailsService");
        http.authorizeRequests()
                // 配置需要放过的资源
                .antMatchers("/login.html","/css/**","/img/**")
                .permitAll()
                .antMatchers("/**")
                .hasAnyRole("USER")
                .anyRequest()
                .authenticated()
                .and()
                // 配置登录表单相关的信息
                .formLogin()
                // 指定自定义的登录页面
                .loginPage("/login.html")
                .loginProcessingUrl("/login") // 表单提交的登录地址
                .defaultSuccessUrl("/home.html")
                .permitAll()
                .and()
                .rememberMe() // 放开 记住我 的功能
                .userDetailsService(userDetailsService)
//                .tokenRepository(persistentTokenRepository()) // 持久化
                .and().csrf().disable();

        // 避免iframe同源无法登录iframe
        http.headers().frameOptions().sameOrigin();

        http.authenticationProvider(new CustomAuthenticationProvider());
        return http.build();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/images/**", "/js/**", "/webjars/**", "/login.html", "/css/**", "/img/**");
    }

}
